package ThreadCommunicate;

import lombok.SneakyThrows;
import lombok.extern.slf4j.Slf4j;

import java.time.Instant;
import java.util.Date;

@Slf4j(topic = "c.TestTestInterrupt")
public class TestInterrupt {
    @SneakyThrows
    public static void main(String[] args) {

        testInterrupt();
        Thread t2 =new Thread(()->{
            log.debug(System.currentTimeMillis()+"  sleep....");
            try {
                Thread.sleep(5000);//sleep ,wait, join会清空打断标记
            } catch (InterruptedException e) {
                throw new RuntimeException(e);
            }
        },"t2");

        t2.start();
        Thread.sleep(2000);
        log.debug(System.currentTimeMillis()+"  interrupt....");
        t2.interrupt();
        log.debug("打断标记{}",t2.isInterrupted());

    }

    public static void testInterrupt() throws InterruptedException {
        Thread t1 =new Thread(()->{
            while(true){
                if(Thread.currentThread().isInterrupted()){
                    log.debug("被打断，退出");
                    break;
                }
            }
        },"t1");
        t1.start();
        Thread.sleep(1000);
        log.debug("t1 is innterupt...");
        t1.interrupt();
    }
}
